package com.exercise.mddocs;

import com.github.cosycode.common.ext.hub.Throws;
import com.github.cosycode.common.util.io.FileSystemUtils;
import com.github.cosycode.common.util.io.IoUtils;
import com.github.cosycode.common.util.otr.PrintTool;
import com.github.cosycode.imgtool.util.PathUtils;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2022/1/7
 * </p>
 *
 * @author CPF
 * @since 1.0
 **/
public class DocsifyIndexGenerator {

    private static final String pathname = "/Users/pengfchen/git/git-note/ccoe-runbook";

    /**
     * 获取一个目录下 所有的 md 文件, 根据这些文件生成一个 docsify 的文档目录
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        final File rootPath = new File(pathname);

        final String canonicalRootPath = Throws.sup(rootPath::getCanonicalPath).logThrowable().value();

        List<Item> mdFileList = new ArrayList<>();
        // 获取根目录下 md 文件
        FileSystemUtils.findFileFromDir(rootPath, f -> f.getName().endsWith(".md"), true, f -> {
            final String parent = f.getParent();
            final String canonicalPath = Throws.sup(f::getCanonicalPath).logThrowable().value();
            final String name = f.getName();
            mdFileList.add(new Item(parent, name, canonicalPath));
        });

        final Map<String, List<Item>> collect =
                mdFileList.stream().collect(Collectors.groupingBy(Item::getParentPath, LinkedHashMap::new, Collectors.toList()));

        StringBuilder sb = new StringBuilder();
        sb.append("\n");

        final String txt = "{}* [{}]({})\n";
        collect.forEach((key, val) -> {
            final String strip = key.replace(canonicalRootPath, "");
            sb.append(StringUtils.repeat(' ', StringUtils.countMatches(strip, "\\") * 3));
            sb.append("* **").append(strip).append("**\n");
            for (Item item : val) {
                final String replace = item.getFilePath().replace(canonicalRootPath, "");
                final String name = item.getFileName().replace(".md", "");
                final String repeat = StringUtils.repeat(' ', StringUtils.countMatches(replace, "\\") * 3);
                final String format = PrintTool.format(txt, repeat, name, replace);
                sb.append(format);
            }
        });

        final String string = sb.toString();
        IoUtils.writeStringToOutputStream(Files.newOutputStream(Paths.get(canonicalRootPath + PathUtils.FILE_SPLIT_PATTERN + "_index-gene.md")),
                string.replace('\\', '/'), StandardCharsets.UTF_8);
    }

    @Getter
    public static class Item {
        String filePath;
        String parentPath;
        String fileName;

        public Item(String parentPath, String fileName, String filePath) {
            this.parentPath = parentPath;
            this.fileName = fileName;
            this.filePath = filePath;
        }
    }


}
